package com.rambo.weather.mvp;

import com.haibin.retrofit.Response;
import com.haibin.retrofit.Retrofit;
import com.haibin.retrofit.call.Call;
import com.haibin.retrofit.call.Callback;
import com.haibin.retrofit.net.Form;
import com.haibin.retrofit.net.GET;
import com.rambo.baselib.httpapi.WanAdApiService;
import com.rambo.baselib.httpapi.WeatherApiService;
import com.rambo.baselib.model.weather.Weather;
import com.rambo.baselib.model.weather.WeatherApiResult;

import java.io.IOException;

/***********************************************************************
 * <p>@description:
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/18 15:45
 **********************************************************************/
public class WeatherRepository {
    public WeatherApiResult getWeatherNow(String location) {
        WeatherApiResult result = new WeatherApiResult();
        try {
            Response<WeatherApiResult> response = Retrofit.getDefualt(Retrofit.WEATHER_BASE_URL).from(WeatherApiService.class)
                    .getWeatherNow(location, WeatherApiService.KEY).execute();
            if (response.getCode() == 200) {
                result = response.getBody();
            } else {
                result.setCode(response.getCode() + "");
                result.setMsg(response.getBodyString());
            }

        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public WeatherApiResult getWeather3d( String location) {
        WeatherApiResult result = new WeatherApiResult();
        try {
            Response<WeatherApiResult> response = Retrofit.getDefualt(Retrofit.WEATHER_BASE_URL).from(WeatherApiService.class)
                    .getWeather3d(location, WeatherApiService.KEY).execute();
            if (response.getCode() == 200) {
                result = response.getBody();
            } else {
                result.setCode(response.getCode() + "");
                result.setMsg(response.getBodyString());
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }

    public WeatherApiResult getWeatherWarn( String location) {
        WeatherApiResult result = new WeatherApiResult();
        try {
            Response<WeatherApiResult> response = Retrofit.getDefualt(Retrofit.WEATHER_BASE_URL).from(WeatherApiService.class)
                    .getWeatherWarn(location, WeatherApiService.KEY).execute();
            if (response.getCode() == 200) {
                result = response.getBody();
            } else {
                result.setCode(response.getCode() + "");
                result.setMsg(response.getBodyString());
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
